package com.main;

import com.test.UnsafeVariableRunnable;

import java.sql.Time;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * Created by 41579 on 2017/4/16.
 */
public class AppTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newCachedThreadPool();

        //类所和对象锁不互斥
//        SyncObj syncObj = new SyncObj();
//        SyncRunnable syncRunnable = new SyncRunnable(syncObj);
//        SyncRunnable2 syncRunnable2 = new SyncRunnable2(syncObj);
//        executor.execute(syncRunnable);
//        executor.execute(syncRunnable2);
//        Runnable runnable = new ThreadLocalRunnable();
//        for(int i=0;i<10;i++){
//            executor.execute(runnable);
//        }


//        InterruptObj interruptObj = new InterruptObj(System.in);
//        InterruptRunnable interruptRunnable = new InterruptRunnable(interruptObj);
//        Future<?> future = executor.submit(interruptRunnable);
//        Future<?> future2 = executor.submit(interruptRunnable);
//        TimeUnit.SECONDS.sleep(2);
//        future2.cancel(true);
//        executor.shutdown();


//        WhileTrueRunnable whileTrueRunnable = new WhileTrueRunnable();
//        Future future = executor.submit(whileTrueRunnable);
//        executor.shutdown();
//        executor.awaitTermination(10,TimeUnit.SECONDS);
//        executor.shutdownNow();
//        TimeUnit.SECONDS.sleep(10);
        //1.cancel
//        future.cancel(true);
        //2.awaitTermination
//        Boolean flag = executor.awaitTermination(20,TimeUnit.SECONDS);
//        System.out.println(flag);
        //3.shutdownNow
//        executor.shutdownNow();


//        WaitRunnable waitRunnable = new WaitRunnable();
//        executor.submit(waitRunnable);
//        executor.submit(waitRunnable);
//        TimeUnit.SECONDS.sleep(3);
//        synchronized (waitRunnable) {
//            waitRunnable.notifyAll();
//            waitRunnable.notify();
//        }
//        executor.shutdown();

//        BreadInventory inventory = new BreadInventory();
//        Productor<Bread> productor = new Productor<Bread>(inventory);
//        Productor<Bread> productor1 = new Productor<Bread>(inventory);
//        Customer<Bread> customer = new Customer<Bread>(inventory);
//        Customer<Bread> customer1 = new Customer<Bread>(inventory);
//        executor.submit(new ProductorRunnable(productor));
//        executor.submit(new ProductorRunnable(productor1));
//        TimeUnit.SECONDS.sleep(2);
//        executor.submit(new CustomerRunnable(customer));
//        executor.submit(new CustomerRunnable(customer1));
//        executor.shutdown();

//        ConditionRunnable conditionRunnable = new ConditionRunnable();
//        executor.submit(conditionRunnable);
//        executor.submit(conditionRunnable);
//        executor.shutdown();

//        LinkedBlockingDeque<Bread> breadQueue = new LinkedBlockingDeque<Bread>(5);
//        executor.submit(new BlockqueueProductor(breadQueue));
//        executor.submit(new BlockqueueProductor(breadQueue));
//        executor.submit(new BlockqueueCustomer(breadQueue));
//        executor.submit(new BlockqueueCustomer(breadQueue));
//        executor.submit(new BlockqueueCustomer(breadQueue));
//        executor.shutdown();

//        CountDownLatch countDownLatch = new CountDownLatch(10);
//        executor.submit(new CountDownAwaitRunnable(countDownLatch));
//        executor.submit(new CountDownAwaitRunnable(countDownLatch));
//        executor.submit(new CountDownAwaitRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.submit(new CountDownLatchRunnable(countDownLatch));
//        executor.shutdown();

//        CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.submit(new CyclicBarrierRunnable(cyclicBarrier));
//        executor.shutdown();

//        DelayQueue<DelayRunnable> delayQueue = new DelayQueue();
//        delayQueue.add(new DelayRunnable("1",10000));
//        delayQueue.add(new DelayRunnable("4",40000));
//        delayQueue.add(new DelayRunnable("3",30000));
//        delayQueue.add(new DelayRunnable("2",20000));
//        while(delayQueue.size()>0){
//            DelayRunnable tmp = delayQueue.poll();
//            if(tmp!=null){
//                System.out.println(tmp.getName());
//            }
//        }
//        executor.shutdown();

        //yield不会释放锁
//        YieldRunnable t1 = new YieldRunnable();
//        executor.submit(t1);
//        executor.submit(t1);
//        executor.shutdown();

        //非线程安全类测试
//        ThreadGroup threadGroup = new ThreadGroup("unsafeVaribleRunnable");
//        UnsafeVariableRunnable unsafeVariableRunnable = new UnsafeVariableRunnable();
//        for(int i=0;i<1000;i++){
//            Thread t1 =  new Thread(threadGroup,unsafeVariableRunnable);
//            t1.start();
//        }
//        while(true){
//            if(threadGroup.activeCount()==0) {
//                System.out.println(unsafeVariableRunnable.getUnsafeVariableList().size());
//                break;
//            }
//        }




        executor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        executor.shutdown();
        while(!executor.isShutdown()){
            System.out.println("continue...");
        }
        System.out.println("end..");

    }

}
